package com.lime49.lockcrypt;

import com.lime49.lockcrypt.ui.AbstractDialog;
import com.lime49.lockcrypt.ui.CheckBoxList;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedHashMap;
import javax.swing.*;

/**
 *
 */
public class FrmChooseAccounts extends AbstractDialog {
    private CheckBoxList<Account> cblAccounts;
    
    public FrmChooseAccounts(FrmImportExport owner, LinkedHashMap<Account, Boolean> accounts) {
        super(owner, owner.getLanguage().getString("CHOOSEACCOUNTS"), true);
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction() {
            // close the frame when the user presses escape
            public void actionPerformed(ActionEvent e) {
                thisDialog.dispose();
            }
        };
        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        this.getRootPane().getActionMap().put("ESCAPE", escapeAction);

        JPanel content = new JPanel(new GridBagLayout());
            content.setBorder(BorderFactory.createEmptyBorder(3,3,0,3));
            this.setContentPane(content);
        GridBagConstraints c = new GridBagConstraints();
            c.insets = Constants.GLOBALINSETS;
            c.weightx = 1;
            int curY = -1;

        Box boxContents = Box.createVerticalBox();
            this.setContentPane(boxContents);
        cblAccounts = new CheckBoxList<Account>(accounts);
        JScrollPane sclAccounts = new JScrollPane(cblAccounts);
            Utils.setgrid(c,++curY,0,1,1,GridBagConstraints.BOTH,GridBagConstraints.FIRST_LINE_START,1);
            boxContents.add(sclAccounts,c);

        JPanel buttonBox = new JPanel(new FlowLayout(FlowLayout.TRAILING));
            c.weighty = 1;
            Utils.setgrid(c,++curY,0,1,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.FIRST_LINE_START,1);
            boxContents.add(buttonBox,c);
        JButton btnSelectAll = new JButton(owner.getLanguage().getString("SELECTALL"));
            btnSelectAll.setActionCommand("all");
            btnSelectAll.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   cblAccounts.selectAll();
               }
            });
            buttonBox.add(btnSelectAll);
        JButton btnSelectNone = new JButton(owner.getLanguage().getString("SELECTNONE"));
            btnSelectNone.setActionCommand("none");
            btnSelectNone.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   cblAccounts.selectNone();
               }
            });
            buttonBox.add(btnSelectNone);

        JButton btnOk = new JButton(owner.getLanguage().getString("OK"));
            btnOk.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   dispose();
               }
            });
            buttonBox.add(btnOk);
            btnSelectAll.setMinimumSize(Constants.MINSIZE);
            btnSelectAll.setPreferredSize(Constants.PREFSIZE);
            btnSelectAll.setMaximumSize(Constants.MAXSIZE);
            btnSelectNone.setMinimumSize(Constants.MINSIZE);
            btnOk.setMaximumSize(Constants.MAXSIZE);

            this.pack();
            this.setLocationRelativeTo(owner);
            this.setVisible(true);
    }

    public LinkedHashMap<Account, Boolean> getAccounts() {
        return cblAccounts.getItems();
    }
}
